package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInstFunc;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 功能产品 产品服务期限属性处理
 * @Author deng.gang
 * @Date 2021/7/29 上午10:04
 */
public class ProductServicePeriodAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        if (!InstType.PROD_INST_FUNC.equals(inst.getInstType())) {
            return;
        }
        KiteProdInstFunc prodInstFunc = (KiteProdInstFunc) inst;
        // 获取接入产品信息
        KiteProdInst prodInst = querySceneInstService.getProdInst(data.getCustId(), prodInstFunc.getAccProdInstId());
        if (Objects.nonNull(prodInst)) {
            String specId = prodInst.getSpecId();
            List<DcPublic> dcPublicList = DcPublicCache.getByPkey("20210729101", "PROD_SERVICE_PERIOD");
            dcPublicList = dcPublicList.stream().filter(dcPublic -> {
                if (dcPublic.getCodea().contains("," + specId + ",")) {
                    return true;
                }
                return false;
            }).collect(Collectors.toList());
            if (KiteListUtils.isNotEmpty(dcPublicList)) {
                List<String> attrCodes = new ArrayList<>();
                dcPublicList.forEach(dcPublic -> {
                    attrCodes.add(dcPublic.getCodeb());
                });
                attr.getAttrValues().removeIf(attrValue -> !attrCodes.contains(attrValue.getAttrValue()));
            }
        }
        super.after(attr, inst, data);
    }
}
